using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProgrammingEducationSystem.Tools;

namespace ProgrammingEducationSystem.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FileController : ControllerBase
    {
        //上传头像
        [HttpPost("avater")]
        public async Task<IActionResult> UpAvatar(IFormFile file) 
        {
            var path = FileTools.GenerAvatarPath(Path.GetExtension(file.FileName));
            var sourcePath = await FileTools.UpFile(file, path);
            if (sourcePath == null) {
                return BadRequest("头像上传失败");
            }
            return Ok(sourcePath);
        }

        //上传学习资源
        [HttpPost("learnsource")]
        public async Task<IActionResult> UpLearnSource(IFormFile file)
        {
            var path = FileTools.GenerLearnSourcePath(Path.GetExtension(file.FileName));
            var sourcePath = await FileTools.UpFile(file, path);
            if (sourcePath == null)
            {
                return BadRequest("学习资源上传失败");
            }
            return Ok(sourcePath);
        }



    }
}
